package cn.shutdown.demo.thread.deadlock;

/**
 * @author jiangwujie
 * @date 2021/8/23
 */
public class DeadLock {
    public static void main(String[] args) {
        new Thread(()->{
            new DeadLock().resource1();
        }).start();
        new Thread(()->{
            new DeadLock().resource2();
        }).start();
    }

    public synchronized void resource1() {
        synchronized ("resource1") {
            System.out.println("获取资源1");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            resource2();
        }
    }

    public synchronized void resource2() {
        synchronized ("resource2") {
            System.out.println("获取资源2");
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            resource1();
        }
    }
}
